var client = {};
var app = getApp();

// 获取楼盘列表接口
client.getTaskList = function (that) {
  console.log("token   ", app.globalData.token);
  my.request({
    url: app.globalData.url + "8ee319aa-bd12-4d81-89ee-340a344375db",
    method: "POST",
    headers: { "Access-Token": app.globalData.token },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        that.setData({ buildingList: res.data.data });
      } else {
        app.errorHandling("获取楼盘列表失败：", res);
      }
    },
    fail: function (res) {
      my.showToast({ content: "网络错误：" + res.data.errorMsg, duration: 2000 });
    },
    complete: function () { my.stopPullDownRefresh(); }
  });
};

// 选中楼盘再登录接口
client.taskLogin = function (taskId1, that) {
  my.showLoading({ content: '加载中...' });
  my.request({
    url: app.globalData.url + "128f85b3-6b4c-4947-9999-0ec90bf07e23",
    method: "POST",
    data: { taskId: taskId1 },
    headers: { "Access-Token": app.globalData.token },
    dataType: "json",
    success: function (res) {
      if (res.data.succeed == 0) {
        // 如果签署过电子协议书，则直接进入选房大厅页面
        if (res.data.data.isProtocolAgree) {
          my.navigateTo({ url: "../mainHall/mainHall" });
        } else {
          // 如果未签署，则进入楼盘简介页面，会返回6008
          my.navigateTo({ url: "../buildingInfo/buildingInfo" });
        }

        my.setStorageSync({
          key: 'taskInfo',
          data: {
            taskName: res.data.data.taskName,
            buildLocation: res.data.data.buildLocation,
            preSaleSum: res.data.data.preSaleSum,
            developerName: res.data.data.developerName,
            selStartTime: res.data.data.selStartTime
          }
        });
      } else {
        my.showToast({ content: "进入楼盘详情失败：" + res.data.errorMsg, duration: 2000 });
      }
    },
    fail: function (res) {
      my.showToast({ content: "网络错误：" + res.data.errorMsg, duration: 2000 });
    },
    complete: function () {
      my.hideLoading();
      that.setData({ secondClick: true });
    }
  });
};

module.exports = { client: client };